<?php

namespace app\index\model;

use think\Model;

class ChannelInfo extends Model
{
    public static function getRates($channelId)
    {
        return self::where(['id' => $channelId])->field('jf_income_rate,channel_income_rate,share_income_rate,user_income_rate')->find();
    }

    public static function getSubscribeTemplate($channelId)
    {
        $ci = self::where(['id' => $channelId])->field('subscribe_template')->find();
        
        return $ci === FALSE ? '' : $ci->subscribe_template;
    }

    public static function getReplyTemplate($channelId)
    {
        return self::where(['id' => $channelId])->field('reply_template,reply_fail_template,reply_coupon_template')->find();
    }

    public static function getUserRates($channelId, $price, $rate, $couponMoney)
    {
        $rates = [0, 0.1, 0.15, 0.2, 0.25, 0.3];
        $income = $price * $rate;
        $rateIndex = 5;
        if($income < 1)
            $rateIndex = 0;
        elseif($income < 5)
            $rateIndex = 1;
        elseif($income < 10)
            $rateIndex = 2;
        elseif($income < 20)
            $rateIndex = 3;
        elseif($income < 50)
            $rateIndex = 4;

        $userRate = $rate * 0.875;
        $userRate -= $userRate * $rates[$rateIndex];
        $userRate = round($userRate, 4);

        return ['user_income' => round($price * $userRate, 2), 'user_income_rate' => $userRate, 'coupon_income' => round(($price - $couponMoney) * $userRate, 2)];
    }
}
